package com.lfg.leetcode.leetcode200;

import java.util.ArrayList;
import java.util.List;

public class No216combinationSum3 {

    public static void main(String[] args) {
        List<List<Integer>> ret = combinationSum3(3, 9);
        System.out.println(ret);
    }

    public static List<List<Integer>> combinationSum3(int k, int n) {

        List<List<Integer>> ret = new ArrayList<>(n);
        if (n >= 9 * k || n < 1) {
            return ret;
        }
        combinationSum3(k, 1, ret, new ArrayList<Integer>(k), n);
        return ret;
    }

    public static void combinationSum3(int cnt, int index, List<List<Integer>> ret, List<Integer> data, int total) {
        int size = data.size();
        if (size > cnt) {
            return;
        }
        for (int i = index; i < 10; i++) {
            int num = total - i;
            if (size == cnt - 2) {
                if (num <= i) {
                    return;
                } else if (num < 10) {
                    data.add(i);
                    data.add(num);
                    ret.add(new ArrayList<>(data));
                    data.remove(data.size() - 1);
                    data.remove(data.size() - 1);
                }
            } else {
                data.add(i);
                combinationSum3(cnt, i + 1, ret, data, num);
                data.remove(data.size() - 1);
            }
        }
    }
}
